
function toProductionDetailPage(id){
    var productDetailPageUrl=basePath()+"res/details_page.html?access_token="+access_token+"&pid=";
    window.location.href=productDetailPageUrl+id;
}

function toProductionLimit(id){
    var productDetailPageUrl=basePath()+"res/xsgpage.html?access_token="+access_token+"&pid=";
    window.location.href=productDetailPageUrl+id;
}

$(function () {
    
//  console.log("get in ");
    getShowBanner();
    getLimitProduct(0);
    getConcentrateProduct(0);
    getAllProduct(0);
    

});
window.onscroll=function(){
//     imglocation('container','box');
    // if(load()){
        getAllProduct(num);
    // }

};

//获取banner列表
function getShowBanner(){
//  console.log("1111");
    $.ajax({
        url:"/yangcangclub/v1/banner/show/list?pageNum=1&pageSize=10",
        type:'GET',
        dataType:"json",
        success:function(data){
//      	console.log(data)
            if(data.code == 1000){
                var _json=window.eval(data).data.list;
                var len=_json.length;
                var _html = "";
                for(var i=0;i<len;i++){
                    _html+="<div class=\"banner-slide swiper-slide\">";
                    var hrefStr="javascript:void(0);";
                    if (_json[i]["url"]!="") hrefStr=_json[i]["url"];
                    _html+="<a href=\""+hrefStr+"\"><img src=\""+_json[i]["image"]+"\"></a>";
                    _html+="</div>"	;
                }
                document.getElementById("swiperlist").innerHTML=_html;
                init();
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
}    
function init(){
    var swiper = $("#banner-cont.swiper-container").swiper({
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
}

//获取限购商品信息1条
function getLimitProduct(orderSort){
    $.ajax({
        url:"/yangcangclub/v1/merchandise/list/show?access_token="+access_token+"&type=3&orderSort="+orderSort+"&pageSize=1",
        type:'GET',
        dataType:"json",
        success:function(data){
//      	console.log(data);
            if(data.code == 1000){
                data=data.data;
                if(data.list==undefined||data.list.length==0){
                	$("#content").hide();
                	return;
                }
                $("#limitPBanner").html('<a href="javascript:toProductionLimit('+data.list[0].id+');">'+
                '<img src="'+data.list[0].bannerUrl+'" ></a>');
                $("#limitPName").html('<p>'+data.list[0].name+'</p>');
                var html='<span class="hour">限时价</span><span class="Price">￥'+data.list[0].unitPrice+'.00</span>';
                    if(data.list[0].showUnitPrice>0) html+='<p class="yprice">原价<s>￥'+data.list[0].showUnitPrice+'.00</s></p>';
                $("#limitPPrice").html(html);
                $("#limitBuyBtn").attr("onclick",'toProductionLimit('+data.list[0].id+');');
                // 计算时间
                var now=new Date(data.now.replace(/-/g,"/"));
//              console.log(now);
                var start=new Date(data.list[0].startTime.replace(/-/g,"/"));
//              console.log(start);
                var end=new Date(data.list[0].endTime.replace(/-/g,"/"));
//               console.log(end);
                var stat=data.sellStatue;//1为可以卖，2为时间未到
                if(stat==2){
                    t=start.getTime()-now.getTime();
                    $("#limitBuyBtn").html("<p>敬请期待</p>");
                }else{
                    t=end.getTime()-now.getTime();
//                  console.log(t);
                }
            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
}

//获取精选商品列表，2条
function getConcentrateProduct(orderSort){
    $.ajax({
        url:"/yangcangclub/v1/merchandise/list/show?access_token="+access_token+"&type=4&orderSort="+orderSort+"&pageSize=5",
        type:'GET',
        dataType:"json",
        success:function(data){
            if(data.code == 1000){
                var list = data.data.list;
                $.each(list, function(index,val){
                    var item = '';
                    item+='<div class="s-left swiper-slide">';
                    item+='<div class="img-left">';
                    item+='<a href="javascript:toProductionDetailPage('+list[index].id+');" class="img">';
                    item+='<img src="'+list[index].bannerUrl+'"></a>';
                    item+='<div class="img-text">';
                    item+='<p>'+list[index].name+'</p></div>';
                    item+='<div class="img-text1"><p>￥'+list[index].unitPrice+'.00</p></div>';
                    item+='</div>';
                    item+='</div>';
                    $("#s-img").append(item);
                });
                $("#concentrateList.swiper-container").swiper({
                    slidesPerView:3,
                    paginationClickable: true,
                    scrollbarHide:false,
                    slidesOffsetAfter : 100,
                    spaceBetween: 0
                });

                // if(n>0){
                //     var left='<a href="javascript:toProductionDetailPage('+data.list[0].id+');" class="img">'+
                //         '<img src="'+data.list[0].bannerUrl+'"></a>'+
                //         '<div class="img-text">'+
                //         '<p>'+data.list[0].name+'</p></div>'+
                //         '<div class="img-text1"><p>￥'+data.list[0].unitPrice+'</p></div>';
                //     $(".img-left").html(left);
                //     $(".img-right").html(left);
                // }
                // if(n>1) {
                //     var right = '<a href="javascript:toProductionDetailPage('+data.list[1].id+');" class="img">' +
                //         '<img src="' + data.list[1].bannerUrl + '"></a>' +
                //         '<div class="img-text">' +
                //         '<p>' + data.list[1].name + '</p></div>' +
                //         '<div class="img-text1"><p>￥' + data.list[1].unitPrice + '</p></div>';
                //     $(".img-right").html(right);
                // }
            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
}

var num=0;
var currn=true;
//获取推荐商品分页
function getAllProduct(orderSort){
    if(!currn){return ;}
    currn=false;
//  console.log("get in "+orderSort);
    $.ajax({
        url:"/yangcangclub/v1/merchandise/list/show?access_token="+access_token+"&type=0&orderSort="+orderSort+"&pageSize=10",
        type:'GET',
        dataType:"json",
        success:function(data){
            if(data.code == 1000){
                data=data.data;
//              console.log(data)
                // var html="";
                if(data==undefined||data.list==undefined||data.list.length==0){ return;}
                var content=document.getElementById('container');
                for(var i = 0 ; i <data.list.length; i ++){
                    var ccContent=document.createElement('div');
                    ccContent.className='box';
                    $(ccContent).attr("onclick",'toProductionDetailPage('+data.list[i].id+');');
                    content.appendChild(ccContent);
                    var imgWarp=document.createElement('div');
                    imgWarp.className='box_img';
                    ccContent.appendChild(imgWarp);
                    var img=document.createElement('img');
                    img.src=data.list[i].bannerUrl;//
                    imgWarp.appendChild(img);
                    var imgtext = document.createElement('div');
                    imgtext.className='imgtext';
                    ccContent.appendChild(imgtext);
                    var jpgtext = document.createElement('p');
                    jpgtext.innerText = data.list[i].name;//
                    imgtext.appendChild(jpgtext);
                    var imgtext1 = document.createElement('div');
                    imgtext1.className='imgtext1';
                    ccContent.appendChild(imgtext1);
                    var jpgtext1 = document.createElement('p');
                    jpgtext1.innerText = "￥"+data.list[i].unitPrice+'.00';//
                    imgtext1.appendChild(jpgtext1);
                    
                    num=data.list[i].orderSort;
                }

//              imglocation('container','box');

//              console.log("get in "+$("#container").html());

//                imglocation('container','box');

            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
            currn=true;
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
}

//

//$(function(){
//	$('#s-img').children(0).css('margin-left','0.46875rem');
//})









